package com.app.service.Impl;

import com.app.mapper.BannerMapper;
import com.app.model.ActivityModel;
import com.app.model.AreaModel;
import com.app.model.BannerModel;
import com.app.service.AreaService;
import com.app.service.BannerService;
import com.app.util.CommonHelp;
import com.app.vo.BannerVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private AreaService areaService;

    @Override
    public void insert(BannerModel banner) {
        banner.setCreateTime(CommonHelp.getNowTimestamp());
        bannerMapper.insert(banner);
    }

    @Override
    public void updateById(BannerModel banner) {
        banner.setUpdateTime(CommonHelp.getNowTimestamp());
        bannerMapper.updateById(banner);
    }

    @Override
    public void deleteBannerById(Integer id) {
        bannerMapper.deleteById(id);
    }

    @Override
    public BannerVo selectBannerList(Integer page, Integer limit, String name) {
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        List<BannerModel> bannerModels = bannerMapper.selectBannerList(page, limit, name);
        Integer count = bannerMapper.selectCount(null);
        BannerVo vo = new BannerVo();
        vo.setList(bannerModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public BannerVo selectBanner() {
        List<BannerModel> bannerModels = bannerMapper.selectList(null);
        List<BannerModel> banner = new ArrayList<>();
        for (int i = 0; i < bannerModels.size(); i++) {
            BannerModel bannerModel = new BannerModel();
            if (bannerModels.get(i).getStatus() == 1) {
                bannerModel.setId(bannerModels.get(i).getId());
                bannerModel.setUrl(bannerModels.get(i).getUrl());
                banner.add(bannerModel);
            }
        }
        BannerVo vo = new BannerVo();
        vo.setList(banner);
        vo.setCount(banner.size());
        return vo;
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        BannerModel bannerModel = new BannerModel();
        bannerModel.setId(id);
        bannerModel.setStatus(status);
        bannerMapper.updateById(bannerModel);
    }
}
